﻿
@{
    ViewBag.Title = "PEEquipFeeMainlyView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>材料成本</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入规格型号" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            @*<button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增引入</button>*@
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" id="UpLoadExcel"><i class="layui-icon">&#xe61f;</i>导入表格</button>
            <a href="~/Content/Templet/用户工程ERP模板.xlsx" download="用户工程ERP模板.xlsx"><label style="display:block;float:left;cursor:pointer" class="layui-form-label">表头模板</label></a>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;margin-left:10px;" id="BatchDel"><i class="layui-icon">&#xe640;</i>批量删除</button>
        </div>
    </div>
</form>
<div id="QuoteEquipFeeLayer" style="display:none;">
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">搜索</label>
            <div class="layui-input-block">
                <input type="text" id="QuoteSearchText" name="Title1" required lay-verify="required" onkeypress="QuoteGetKey()" placeholder="输入物料编码或规格名称" class="layui-input" style="width:300px;float:left;">
                <input type="text" value="" style="display:none" />
                <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="QuoteSearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="QuoteBtnAdd"><i class="layui-icon">&#xe61f;</i>引入选中项</button>
            </div>
        </div>
    </form>
    <table id="QuoteEquipFeeTable" lay-filter="QuoteEquipFeeTablefilter"></table>
</div>
<table id="MaterialFeeMainlyTable" lay-filter="MaterialFeeMainlyTablefilter"></table>

<script type="text/template" id="LayerProgBar">
    <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
    </div>
</script>

<script type="text/javascript">
    var table;
    var TabIndex = 0;
    var PreEstId = parent.$("#PreEstId").val();
    var PreEstType = parent.$("#ProjectType").val();

    layui.use(['form', 'table', 'upload', 'layer', 'element'], function () {
        var upload = layui.upload;
        var element = layui.element;
        var layer = layui.layer;
        var form = layui.form;
        table = layui.table;

        $("#BatchDel").on("click", function () {
            var checkStatus = table.checkStatus('MaterialFeeMainlyTable');
            var data = checkStatus.data;
            if (data.length < 1) {
                layer.msg('至少选择一行数据', { icon: 3, time: 1500 });
                return;
            }
            var arr = new Array();
            for (var i = 0; i < data.length; i++) {
                arr[i] = data[i].PEMaterialPriceId;
            }

            $.ajax({
                type: "POST",
                url: "/Estimate/PEMaterialFeeBatchDel",
                //contentType: "application/json",
                //dataType: "json",
                async: false,
                data: { "PEMaterialPriceId": arr },
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('删除成功"', { icon: 6, time: 1000 });
                        table.reload("MaterialFeeMainlyTable", {
                            //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                        });
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });




        $("#BtnAdd").on("click", function () {
            layerindex = layer.open({
                type: 1,
                title: ['协议库存数据', 'font-size:18px;'],
                area: '1020px',
                offset: 'rt',
                content: $("#QuoteEquipFeeLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                success: function (layero, index) {
                    //$("#EditFrom input[name='TestFeeId']").val("");
                }
            });
        });

        table.render({
            elem: '#QuoteEquipFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/EquipFeeMainlyTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "MaterialId", Order: "Desc", Condition: " and IsDelete<>1" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'Code', title: '物料编码', width: 120 }
                , { field: 'Specification', title: '规格名称', width: 300 }
                , { field: 'Unit', title: '单位', width: 75 }
                , { field: 'Price', title: '协议单价', width: 120 }
                , { field: 'Quantity', title: '引入数量', width: 120, edit: 'text' }
                , { field: 'QuoteMoney', title: '引入单价', width: 120, edit: 'text'}

            ]]
        });

        table.on('edit(QuoteEquipFeeTablefilter)', function (obj) {
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('QuoteEquipFeeTable');
                return;
            }
        });

        $("#QuoteBtnAdd").on("click", function () {
            var checkStatus = table.checkStatus('QuoteEquipFeeTable');
            var data = checkStatus.data;    //这里得到的data是array类型
            if (checkStatus.data.length == 0) { return; }
            //alert(JSON.stringify(data));
            //先检查内容
            data.forEach(function (item, index) {
                item["PreEstId"] = PreEstId;
                item["PreEstType"] = PreEstType;
            });

            $.ajax({
                type: "POST",
                url: "/Estimate/MaterialFeeMainlyAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('引入成功', { icon: 6, time: 1000 });
                        table.reload('MaterialFeeMainlyTable');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });
        //////////////////////////////////////////////////////////////////////


        table.render({
            elem: '#MaterialFeeMainlyTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PEMaterialTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'Code', title: '编号', width: 120 }
                , { field: 'Specification', title: '规格型号', width: 350 }
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text' }
                , {
                    title: '单价', width: 120,
                    templet: function (d) {
                        return (d.SumTax / d.Quantity);
                    }
                }
                , { field: 'SumTax', title: '合价', width: 120, edit: 'text' }
                , {
                    field: "operation", title: "操作", width: 80, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }

            ]]
        });

        table.on('edit(MaterialFeeMainlyTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "SumTax") {
                    eval(data.SumTax);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('MaterialFeeMainlyTable');
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/WorkQuantitiesUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(obj.data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('MaterialFeeMainlyTable');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        table.on("tool(MaterialFeeMainlyTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.EquipName + " " + data.Specification + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/PEMaterialFeeDel",
                            async: false,
                            data: { "PEMaterialPriceId": data.PEMaterialPriceId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("MaterialFeeMainlyTable", {

                                    });
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });

        var uploadInst = upload.render({
            elem: '#UpLoadExcel' //绑定元素
            , url: '/Estimate/MaterialFeeMainlyUpload/' //上传接口
            , accept: 'file'
            , size: 1024000
            , exts: "xls|xlsx"
            , data: { "PreEstId": PreEstId, "PreEstType": PreEstType }
            , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                LayerIndex = layer.open({
                    type: 1,
                    title: '进度条',
                    area: ['700px', '200'],
                    content: $("#LayerProgBar").html(),
                    closeBtn: 0

                });
                //然后需要重新渲染一下上传进度条
                element.init();
            }
            , progress: function (e, percent) {
                console.log("进度：" + percent + '%');
                element.progress('progressBar', percent + '%');
            }
            , done: function (msg, index, upload) {
                //上传完毕回调
                layer.close(LayerIndex);
                if (msg.result != "OK") {
                    layer.msg(msg.result, { icon: 6, time: 10000 });
                }
                else {
                    layer.msg('上传成功', { icon: 6, time: 2000 });
                    table.reload("MaterialFeeMainlyTable", {
                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                    });
                }

            }
            , error: function () {
                //请求异常回调
                alert(msg.result);
            }
        });

    });


    //引用表搜索
    function QuoteGetKey() {
        if (event.keyCode == 13) {
            QuoteSearchFunc();
        }
    }
    function QuoteSearchFunc() {
        var text = $("#QuoteSearchText").val();
        if (text == "") {
            table.reload("QuoteEquipFeeTable", {
                where: {
                    SortName: "MaterialId", Order: "Desc", Condition: " and IsDelete<>1"
                }
            });
        }
        else {
            table.reload("QuoteEquipFeeTable", {
                where: {
                    SortName: "MaterialId", Order: "Desc", Condition: " and IsDelete<>1 and (Specification like '%" + text + "%' or Code like '%" + text + "%')"
                }
            });
        }
    }


    //数据表搜索回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }
    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            if (TabIndex == 0) {
                table.reload("MaterialFeeMainlyTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }
        }
        else {
            if (TabIndex == 0) {
                table.reload("MaterialFeeMainlyTable", {
                    where: {
                        SortName: "PEMaterialPriceId", Order: "Desc", Condition: " and IsDelete<>1 and (Code like '%" + text + "%' or Specification like '%" + text + "%') and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                    }
                });
            }
        }
    }



</script>